package cloud.seri.gateway.web.rest

import cloud.seri.gateway.SeriGatewayApp
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.setup.MockMvcBuilders

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

/**
 * Integration tests for the [ClientForwardController] REST controller.
 */
@RunWith(SpringRunner::class)
@SpringBootTest(classes = [SeriGatewayApp::class])
class ClientForwardControllerIT {

    private lateinit var restMockMvc: MockMvc

    @Before
    fun setup() {
        val clientForwardController = ClientForwardController()
        val logsResource = LogsResource()
        this.restMockMvc = MockMvcBuilders
            .standaloneSetup(clientForwardController, logsResource)
            .build()
    }

    @Test
    @Throws(Exception::class)
    fun getBackendEndpoint() {
        restMockMvc.perform(get("/management/logs"))
            .andExpect(status().isOk)
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
    }

    @Test
    @Throws(Exception::class)
    fun getClientEndpoint() {
        val perform = restMockMvc.perform(get("/non-existant-mapping"))
        perform
            .andExpect(status().isOk)
            .andExpect(forwardedUrl("/"))
    }

    @Test
    @Throws(Exception::class)
    fun getNestedClientEndpoint() {
        restMockMvc.perform(get("/admin/user-management"))
            .andExpect(status().isOk)
            .andExpect(forwardedUrl("/"))
    }
}
